package com.example.fenye.controller.forum;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.example.fenye.entity.forum.ForumUser;
import com.example.fenye.service.forum.ForumUserService;
import com.example.fenye.util.R;


@Controller
@RequestMapping(value="/forum/login")
public class LoginController {
	
	
	@Resource
	ForumUserService forumUserService;
	
	
	@GetMapping(value ="/toLogin")
	public ModelAndView toLogin(HttpServletRequest request){
		return new ModelAndView("forum/login");
	}
	
	@ResponseBody
    @RequestMapping(value="/doLogin")
    public R Login(HttpServletRequest request, HttpSession session, String name, String password){
    	
    	//根据账号判断数据库中是否存在该用户
		//boolean isValidUser = forumUserService.validateUser(name,password);
		
		ForumUser user = forumUserService.getForumUserByNamePass(name, password); 
    	//如果不存在该用户
    	if (user==null) {
    		//返回登录页面
    		return R.error("用户名或密码错误");
 
    	//如果存在该用户
    	} else {
    		session.setAttribute("userLogin",user);
    		return R.ok();
    	}
	}
}
